﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package org.aswing {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import org.aswing.event.*;
    import org.aswing.util.*;
    import flash.ui.*;

    public class FocusManager {

        private static var managers:WeakMap = new WeakMap();
        private static var defaultTraversalEnabled:Boolean = true;

        private var oldFocusOwner:Component;
        private var focusOwner:Component;
        private var popups:ArrayList;
        private var activeWindow:JWindow;
        private var traversalEnabled:Boolean = true;
        private var traversalDefault:Boolean = true;
        private var stage:Stage;
        private var focusRect:Sprite;
        private var inited:Boolean;
        private var defaultPolicy:FocusTraversalPolicy;
        private var traversing:Boolean;
        private var focusPaintedComponent:Component;

        public function FocusManager(_arg1:Stage){
            this.traversing = false;
            this.inited = false;
            this.defaultPolicy = new ContainerOrderFocusTraversalPolicy();
            this.popups = new ArrayList();
            this.init(_arg1);
        }
        public static function getManager(_arg1:Stage):FocusManager{
            if (_arg1 == null){
                return (null);
            };
            var _local2:FocusManager = managers.getValue(_arg1);
            if (_local2 == null){
                _local2 = new FocusManager(_arg1);
                managers.put(_arg1, _local2);
            };
            return (_local2);
        }
        public static function setManager(_arg1:Stage, _arg2:FocusManager):void{
            if (_arg1 == null){
                throw (new Error("theStage can't be null!"));
            };
            if (_arg2 == null){
                _arg2 = new FocusManager(_arg1);
            };
            var _local3:FocusManager = managers.getValue(_arg1);
            if (_local3 != _arg2){
                if (_local3 != null){
                    _local3.uninit();
                };
                managers.put(_arg1, _arg2);
            };
        }
        public static function setDefaultTraversalEnabled(_arg1:Boolean):void{
            defaultTraversalEnabled = _arg1;
        }
        public static function isDefaultTraversalEnabled():Boolean{
            return (defaultTraversalEnabled);
        }

        private function __referenceEvent(_arg1:Event):void{
        }
        public function init(_arg1:Stage):void{
            if (!this.inited){
                this.stage = _arg1;
                this.inited = true;
                this.stage.addEventListener(FocusEvent.KEY_FOCUS_CHANGE, this.__onKeyFocusChange, false, 0, true);
                this.stage.addEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, this.__onMouseFocusChange, false, 0, true);
                this.stage.addEventListener(KeyboardEvent.KEY_DOWN, this.__onKeyDown, false, 0, true);
                this.stage.addEventListener(KeyboardEvent.KEY_UP, this.__onKeyUp, false, 0, true);
                this.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.__onMouseDown, false, 0, true);
                this.focusRect = new Sprite();
                this.focusRect.mouseEnabled = false;
                this.focusRect.visible = false;
                this.stage.addChild(this.focusRect);
                this.stage.addEventListener(Event.DEACTIVATE, this.__referenceEvent);
            };
        }
        public function getPopupsVector():ArrayList{
            return (this.popups);
        }
        public function moveFocusRectUpperTo(_arg1:Component):Sprite{
            if (this.focusPaintedComponent != _arg1){
                if (this.focusPaintedComponent != null){
                    this.removeistenerToFocusPaintedComponent();
                };
                this.focusPaintedComponent = _arg1;
                this.addListenerToFocusPaintedComponent();
            };
            DepthManager.bringToTop(this.focusRect);
            var _local2:Point = _arg1.localToGlobal(new Point());
            this.focusRect.x = _local2.x;
            this.focusRect.y = _local2.y;
            return (this.focusRect);
        }
        private function addListenerToFocusPaintedComponent():void{
            this.focusPaintedComponent.addEventListener(MovedEvent.MOVED, this.__focusPaintedComMoved);
            this.focusPaintedComponent.addEventListener(ResizedEvent.RESIZED, this.__focusPaintedComResized);
            this.focusPaintedComponent.addEventListener(Event.REMOVED_FROM_STAGE, this.__focusPaintedComRemoved);
        }
        private function removeistenerToFocusPaintedComponent():void{
            if (this.focusPaintedComponent != null){
                this.focusPaintedComponent.removeEventListener(MovedEvent.MOVED, this.__focusPaintedComMoved);
                this.focusPaintedComponent.removeEventListener(ResizedEvent.RESIZED, this.__focusPaintedComResized);
                this.focusPaintedComponent.removeEventListener(Event.REMOVED_FROM_STAGE, this.__focusPaintedComRemoved);
                this.focusPaintedComponent = null;
            };
        }
        private function __focusPaintedComRemoved(_arg1:Event):void{
            this.focusRect.graphics.clear();
            this.removeistenerToFocusPaintedComponent();
        }
        private function __focusPaintedComMoved(_arg1:MovedEvent):void{
            var _local2:int;
            var _local3:int;
            if (this.focusRect.visible){
                _local2 = (_arg1.getNewLocation().x - _arg1.getOldLocation().x);
                _local3 = (_arg1.getNewLocation().y - _arg1.getOldLocation().y);
                this.focusRect.x = (this.focusRect.x + _local2);
                this.focusRect.y = (this.focusRect.y + _local3);
            };
        }
        private function __focusPaintedComResized(_arg1:ResizedEvent):void{
            if (this.focusRect.visible){
                this.focusPaintedComponent.paintFocusRect(true);
            };
        }
        public function uninit():void{
            if (this.stage != null){
                this.stage.removeEventListener(FocusEvent.KEY_FOCUS_CHANGE, this.__onKeyFocusChange, false);
                this.stage.removeEventListener(FocusEvent.MOUSE_FOCUS_CHANGE, this.__onMouseFocusChange, false);
                this.stage.removeEventListener(KeyboardEvent.KEY_DOWN, this.__onKeyDown, false);
                this.stage.removeEventListener(KeyboardEvent.KEY_UP, this.__onKeyUp, false);
                this.stage.removeEventListener(MouseEvent.MOUSE_DOWN, this.__onMouseDown, false);
                this.stage = null;
                this.focusOwner = null;
                this.activeWindow = null;
                this.defaultPolicy = null;
                this.focusPaintedComponent = null;
                if (this.focusRect.parent){
                    this.focusRect.parent.removeChild(this.focusRect);
                };
                this.focusRect = null;
                this.inited = false;
                this.oldFocusOwner = null;
                this.traversing = false;
            };
        }
        private function __onMouseDown(_arg1:MouseEvent):void{
            this.setTraversing(false);
        }
        private function __onMouseFocusChange(_arg1:FocusEvent):void{
            var _local2:InteractiveObject;
            if (this.focusOwner != null){
                _local2 = (_arg1.relatedObject as InteractiveObject);
                if (((((AsWingManager.isPreventNullFocus()) && ((((_local2 == null)) || (!((((_local2 is TextField)) || (_local2.tabEnabled)))))))) || ((((_local2 is Component)) && (!(Component(_local2).isFocusable())))))){
                    _arg1.preventDefault();
                };
            };
        }
        private function __onKeyFocusChange(_arg1:FocusEvent):void{
            if (!this.isTraversalEnabled()){
                return;
            };
            if (this.focusOwner != null){
                _arg1.preventDefault();
            };
            if (_arg1.keyCode != Keyboard.TAB){
                return;
            };
            this.setTraversing(true);
            if (_arg1.shiftKey){
                this.focusPrevious();
            } else {
                this.focusNext();
            };
        }
        private function __onKeyDown(_arg1:KeyboardEvent):void{
            if (this.focusOwner != null){
                this.focusOwner.fireFocusKeyDownEvent(_arg1);
            };
        }
        private function __onKeyUp(_arg1:KeyboardEvent):void{
            if (this.focusOwner != null){
                this.focusOwner.fireFocusKeyUpEvent(_arg1);
            };
        }
        public function isTraversing():Boolean{
            return (this.traversing);
        }
        public function setTraversing(_arg1:Boolean):void{
            this.traversing = _arg1;
            this.focusRect.visible = _arg1;
            if (!_arg1){
                this.focusRect.graphics.clear();
                this.removeistenerToFocusPaintedComponent();
            };
        }
        public function setTraversalEnabled(_arg1:Boolean):void{
            this.traversalEnabled = _arg1;
            this.setTraversalAsDefault(false);
        }
        public function isTraversalEnabled():Boolean{
            return (this.traversalEnabled);
        }
        public function setTraversalAsDefault(_arg1:Boolean):void{
            this.traversalDefault = _arg1;
        }
        public function isTraversalAsDefault():Boolean{
            return (this.traversalDefault);
        }
        public function getPreviousFocusedComponent():Component{
            return (this.oldFocusOwner);
        }
        public function getFocusOwner():Component{
            return (this.focusOwner);
        }
        public function setFocusOwner(_arg1:Component):void{
            if (this.focusOwner != _arg1){
                this.oldFocusOwner = this.focusOwner;
                this.focusOwner = _arg1;
            };
        }
        public function getActiveWindow():JWindow{
            return (this.activeWindow);
        }
        public function setActiveWindow(_arg1:JWindow):void{
            this.activeWindow = _arg1;
        }
        public function focusNextOfComponent(_arg1:Component):void{
            if (_arg1 != null){
                _arg1.transferFocus();
            };
        }
        public function focusPreviousOfComponent(_arg1:Component):void{
            if (_arg1 != null){
                _arg1.transferFocusBackward();
            };
        }
        public function focusNext():void{
            this.focusNextOfComponent(this.getFocusOwner());
        }
        public function focusPrevious():void{
            this.focusPreviousOfComponent(this.getFocusOwner());
        }
        public function getDefaultFocusTraversalPolicy():FocusTraversalPolicy{
            return (this.defaultPolicy);
        }
        public function setDefaultFocusTraversalPolicy(_arg1:FocusTraversalPolicy):void{
            if (_arg1 != null){
                this.defaultPolicy = _arg1;
            };
        }

    }
}//package org.aswing 
